<?php

namespace falcon5\inc\Base;

use hollisho\helpers\ArrayHelper;

class WPOption
{
    const FC5_API_KEY = 'activityTicket';

    const ORIGINAL_LANGUAGE = 'originalLanguage';

    const ALLOWED_LANGUAGES = 'selectedLanguages';

    const FLAG_SIZE = 'buttonDesign.flagSize';

    const FLAG_STYLE = 'buttonDesign.flagStyle';
    const LANGUAGE_FORMAT = 'languageFormat';

    const OPEN_CACHE_DIR = 'openCacheDir';


    public function delOption()
    {
        return delete_option(Common::PLUGIN_ID);
    }

    public function getOption($key, $default = null)
    {
        $options = get_option(Common::PLUGIN_ID, []);
        $option = ArrayHelper::getValue($options, $key, $default);
        return apply_filters( 'falcon5_get_option', $option, $key, $options );
    }

    public function getApiKey() {
        return $this->getOption(WPOption::FC5_API_KEY);
    }

    public function getOriginalLanguage()
    {
        return $this->getOption(WPOption::ORIGINAL_LANGUAGE);
    }

    public function getAllowedLanguages(): array
    {
        $allowedLanguages = $this->getOption(WPOption::ALLOWED_LANGUAGES);
        foreach ($allowedLanguages as $allowedLanguage) {
            $item=$allowedLanguage;
            $code=strtolower($item["code"]);
            if($item["code"]!=$code){
                $item["code"]=strtolower($item["code"]);
                $allowedLanguages[]=$item; 
            }
            
        }
        
        return $allowedLanguages ? ArrayHelper::getColumn($allowedLanguages, 'code'): [];
    }

    public function getFlagSize()
    {
        return $this->getOption(WPOption::FLAG_SIZE);
    }

    public function getFlagStyle()
    {
        return $this->getOption(WPOption::FLAG_STYLE);
    }

    public function isOpenCacheDir(): bool
    {
        return boolval($this->getOption(WPOption::OPEN_CACHE_DIR));
    }

    public function getUrlMode(): string
    {
        return $this->getOption(WPOption::LANGUAGE_FORMAT, WPUrlMode::URL_MODE_SUBFOLDER);
    }

}